﻿namespace JumpstartApp
{
    using JumpstartApp.Models;

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    using Windows.Storage;

    public static class Data
    {
        static List<ParentBase> s_RealData = null;
        public async static Task<List<ParentBase>> RealData()
        {
            if (s_RealData != null)
                return s_RealData;
            s_RealData = new List<ParentBase>();
            var _Uri = new Uri("ms-appx:///data/data.xml");
            var _File = await StorageFile.GetFileFromApplicationUriAsync(_Uri);
            var _Xml = XElement.Load(_File.Path).ToString();
            var _Bytes = Encoding.Unicode.GetBytes(_Xml);
            using (var _Stream = new MemoryStream(_Bytes))
            {
                var _Serializer = new XmlSerializer(typeof(DataRecord));
                var _Data = (DataRecord)_Serializer.Deserialize(_Stream);

                // resort children
                foreach (var item in _Data.Courses)
                {
                    item.Children.Clear();
                    item.Modules = item.Modules.OrderBy(x => x.Title).ToList();
                    s_RealData.Add(item);
                }
                foreach (var item in _Data.Shows)
                {
                    item.Children.Clear();
                    item.Episodes = item.Episodes.OrderBy(x => x.DateTime).ToList();
                    s_RealData.Add(item);
                }
            }
            return s_RealData;
        }

        static List<ParentBase> s_SampleData = null;
        public static List<ParentBase> SampleData()
        {
            if (s_SampleData != null)
                return s_SampleData;
            s_SampleData = new List<ParentBase>();

            // show
            var _Episodes = Enumerable.Range(1, 30).Select(x => new EpisodeRecord
            {
                Title = string.Format("({0}) Module: The quick brown fox jumps over the lazy dog.", x),
                Description = "Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.",
                Image = "http://media.ch9.ms/ch9/edb0/ebb50974-cbb9-44d6-8448-fd340113edb0/WinStoreAppEssM05_512.jpg",
                Date = DateTime.Now.ToString(),
            });
            for (int i = 0; i < 2; i++)
            {
                var _Show = new ShowRecord
                {
                    Title = string.Format("Show {0}", i),
                    Episodes = new List<EpisodeRecord>(_Episodes.ToArray()),
                };
                s_SampleData.Add(_Show);
            }

            // course
            var _Modules = Enumerable.Range(1, 8).Select(x => new ModuleRecord
            {
                Title = string.Format("({0}) Module: The quick brown fox jumps over the lazy dog.", x),
                Description = "Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.",
                Image = "http://media.ch9.ms/ch9/edb0/ebb50974-cbb9-44d6-8448-fd340113edb0/WinStoreAppEssM05_512.jpg"
            });
            for (int i = 0; i < 2; i++)
            {
                var _Course = new CourseRecord
                {
                    Title = string.Format("Course {0}", i),
                    Date = DateTime.Now.ToString(),
                    Modules = new List<ModuleRecord>(_Modules.ToArray()),
                };
                s_SampleData.Add(_Course);
            }

            return s_SampleData;
        }
    }
}
